home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / pc / SNNSV32.ZIP / SNNSv3.2 / xgui / sources / d3_anageo.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-04-25  |  8.9 KB  |  402 lines

  1. /*****************************************************************************
  2.   File           : d3_anageo.c
  3.   SHORTNAME      : anageo.c 
  4.   SNNS VERSION   : 3.2
  5.  
  6.   PURPOSE        : matrix operations and cube operations
  7.   NOTES          :
  8.  
  9.   AUTHOR         : Ralf Huebner
  10.   DATE           : 1.12.1991
  11.  
  12.   CHANGED BY     : Sven Doering
  13.   IDENTIFICATION : @(#)d3_anageo.c    1.11 3/2/94
  14.   SCCS VERSION   : 1.11
  15.   LAST CHANGE    : 3/2/94
  16.  
  17.              Copyright (c) 1990-1994  SNNS Group, IPVR, Univ. Stuttgart, FRG
  18. ******************************************************************************/
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <ctype.h>
  22. #include <math.h>
  23. #include <string.h>
  24. #include <X11/Xlib.h>
  25. #include <X11/Intrinsic.h>
  26.  
  27. #include "glob_typ.h"
  28. #include "d3_global.h"
  29.  
  30. #include "d3_anageo.ph"
  31.  
  32.  
  33.  
  34. #ifdef _UNUSED_FUNCTIONS_
  35.  
  36. /*****************************************************************************
  37.   FUNCTION : d3_showVector
  38.  
  39.   PURPOSE  : writes a vector to stdout
  40.   RETURNS  : 
  41.   NOTES    :
  42.  
  43. ******************************************************************************/
  44.  
  45. static void d3_showVector (vector v)
  46.  
  47. {
  48.     fprintf (stdout, "%10.5f   %10.5f   %10.5f   %10.5f\n",
  49.              v[0], v[1], v[2], v[3]);
  50. }
  51.  
  52. #endif /* _UNUSED_UNCTIONS_ */
  53.  
  54.  
  55.  
  56.  
  57. /*****************************************************************************
  58.   FUNCTION : e_matrix
  59.  
  60.   PURPOSE  : creates a unit matrix
  61.   RETURNS  : unit matrix
  62.   NOTES    :
  63.  
  64. ******************************************************************************/
  65.  
  66. static void e_matrix (matrix m)
  67. {
  68.     int i, j;
  69.  
  70.     for (i=0; i<4; i++)
  71.       {
  72.         for (j=0; j<4; j++)
  73.           {
  74.             m[i][j] = 0.0;
  75.             if (i==j)
  76.               m[i][j] = 1.0;
  77.           }
  78.       }
  79. }
  80.  
  81.  
  82.  
  83. /*****************************************************************************
  84.   FUNCTION : d3_transMatrix
  85.  
  86.   PURPOSE  : creates a translation matrix
  87.   RETURNS  : translation matrix
  88.   NOTES    :
  89.  
  90. ******************************************************************************/
  91.  
  92. void d3_transMatrix (matrix m, vector v)
  93. {
  94.     e_matrix (m);
  95.     m[0][3] = v[0];
  96.     m[1][3] = v[1];
  97.     m[2][3] = v[2];
  98. }
  99.  
  100.  
  101.  
  102.  
  103. /*****************************************************************************
  104.   FUNCTION : d3_scaleMatrix
  105.  
  106.   PURPOSE  : creates a scale matrix
  107.   RETURNS  : scale matrix 
  108.   NOTES    :
  109.  
  110. ******************************************************************************/
  111.  
  112. void d3_scaleMatrix (matrix m, vector v)
  113. {
  114.     e_matrix (m);
  115.     m[0][0] = v[0];
  116.     m[1][1] = v[1];
  117.     m[2][2] = v[2];
  118. }
  119.  
  120.  
  121. /*****************************************************************************
  122.   FUNCTION : d3_rotateXmatrix
  123.  
  124.   PURPOSE  : creates a rotation matrix arround the X - axis
  125.   RETURNS  : rotation matrix 
  126.   NOTES    :
  127.  
  128. *****************************************************************************/
  129.  
  130. static void d3_rotateXmatrix (matrix m, float phi)
  131.  
  132. {
  133.     float sinus, cosinus;
  134.  
  135.     e_matrix (m);
  136.     sinus = sin (phi);
  137.     cosinus = cos (phi);
  138.     m[1][1] = cosinus;
  139.     m[2][1] = sinus;
  140.     m[1][2] = -sinus;
  141.     m[2][2] = cosinus;
  142. }
  143.  
  144.  
  145.  
  146.  
  147. /*****************************************************************************
  148.   FUNCTION : d3_rotateYmatrix
  149.  
  150.   PURPOSE  : creates a rotation matrix arround the Y - axis
  151.   RETURNS  : rotation matrix
  152.   NOTES    :
  153.  
  154. *****************************************************************************/
  155.  
  156. static void d3_rotateYmatrix (matrix m, float phi)
  157. {
  158.     float sinus, cosinus;
  159.  
  160.     e_matrix (m);
  161.     sinus = sin (phi);
  162.     cosinus = cos (phi);
  163.     m[0][0] = cosinus;
  164.     m[2][0] = sinus;
  165.     m[0][2] = -sinus;
  166.     m[2][2] = cosinus;
  167. }
  168.  
  169.  
  170.  
  171.  
  172.  
  173. /*****************************************************************************
  174.   FUNCTION : d3_rotateZmatrix
  175.  
  176.   PURPOSE  : creates a rotation matrix arround the Z - axis
  177.   RETURNS  : rotation matrix
  178.   NOTES    :
  179.  
  180. ******************************************************************************/
  181. static void d3_rotateZmatrix (matrix m, float phi)
  182. {
  183.     float sinus, cosinus;
  184.  
  185.     e_matrix (m);
  186.     sinus = sin (phi);
  187.     cosinus = cos (phi);
  188.     m[0][0] = cosinus;
  189.     m[1][0] = sinus;
  190.     m[0][1] = -sinus;
  191.     m[1][1] = cosinus;
  192. }
  193.  
  194.  
  195.  
  196.  
  197. /*****************************************************************************
  198.   FUNCTION : d3_multMatrix
  199.  
  200.   PURPOSE  : multplies two matrices
  201.   RETURNS  : the new matrix
  202.   NOTES    : C = A * B
  203.  
  204. ******************************************************************************/
  205. void d3_multMatrix (matrix c, matrix a, matrix b)
  206. {
  207.     int i, j, k;
  208.     float s;
  209.  
  210.     for (i=0; i<4; i++)
  211.       for (j=0; j<4; j++)
  212.         {
  213.           s = 0;
  214.           for (k=0; k<4; k++)
  215.             s += a[i][k] * b[k][j];
  216.           c[i][j] = s;
  217.         };
  218. }
  219.         
  220.  
  221.  
  222. /*****************************************************************************
  223.   FUNCTION : d3_multMatrixVector
  224.  
  225.   PURPOSE  : multplies a matrix with a vector
  226.   RETURNS  : the new vector
  227.   NOTES    :
  228.  
  229. ******************************************************************************/
  230. void d3_multMatrixVector (vector u, matrix m, vector v)
  231. {
  232.     int i;
  233.  
  234.     for (i=0; i<4; i++)
  235.       u[i] = m[i][0] * v[0] + m[i][1] * v[1] + m[i][2] * v[2] + m[i][3];
  236.  
  237.  
  238.  
  239.  
  240.  
  241. /*****************************************************************************
  242.   FUNCTION : d3_normalVector
  243.  
  244.   PURPOSE  : calculates the normal for a polygon
  245.   RETURNS  : the normal
  246.   NOTES    : only the first 3 vectors
  247.  
  248. ******************************************************************************/
  249. void d3_normalVector (vector nv, vector v1, vector v2, vector v3)
  250. {
  251.     int i;
  252.     vector v, w;
  253.  
  254.     for (i=0; i<4; i++) {
  255.         v[i] = v2[i] - v1[i];
  256.         w[i] = v3[i] - v1[i];
  257.     }
  258.     nv[0] = v[1]*w[2] - v[2]*w[1];
  259.     nv[1] = v[2]*w[0] - v[0]*w[2];
  260.     nv[2] = v[0]*w[1] - v[1]*w[0];
  261.     nv[3] = sqrt (nv[0]*nv[0] + nv[1]*nv[1] + nv[2]*nv[2]);
  262. }
  263.  
  264.  
  265.  
  266.  
  267. /*****************************************************************************
  268.   FUNCTION : d3_rotateMatrix
  269.  
  270.   PURPOSE  : creates a rotation matrix arround all axes
  271.   RETURNS  : the rotation matrix
  272.   NOTES    :
  273.  
  274. ******************************************************************************/
  275. void d3_rotateMatrix (matrix m, vector v)
  276. {
  277.     matrix rot_x, rot_y, rot_z, temp;
  278.  
  279.     d3_rotateXmatrix (rot_x,(float) v[0]);
  280.     d3_rotateYmatrix (rot_y,(float) v[1]);
  281.     d3_rotateZmatrix (rot_z,(float) v[2]);
  282.  
  283.     d3_multMatrix (temp, rot_y, rot_x);
  284.     d3_multMatrix (m, rot_z, temp);
  285. }
  286.  
  287.  
  288.  
  289. /*****************************************************************************
  290.   FUNCTION : d3_rotateCube
  291.  
  292.   PURPOSE  : rotates a cube arround all axes
  293.   RETURNS  : the rotated cube
  294.   NOTES    :
  295.  
  296. ******************************************************************************/
  297. void d3_rotateCube (cube c1, vector r, cube c2)
  298. {
  299.     float a, b, c, d, e, f, g, h, j;
  300.     vector si, co;
  301.     int i;
  302.  
  303.     for (i=0; i<3; i++)
  304.       {
  305.          si[i] = sin (r[i]);
  306.          co[i] = cos (r[i]);
  307.       }
  308.     a = co[1] * co[2];
  309.     b = co[1] * si[2];
  310.     c = -si[1];
  311.     d = si[0] * si[1] * co[2] - co[0] * si[2];
  312.     e = si[0] * si[1] * si[2] + co[0] * co[2];
  313.     f = si[0] * co[1];
  314.     g = co[0] * si[1] * co[2] + si[0] * si[2];
  315.     h = co[0] * si[1] * si[2] - si[0] * co[2];
  316.     j = co[0] * co[1];
  317.     for (i=0; i<ANZ_VECS; i++)
  318.       {
  319.         c1[i][0] = c2[i][0] * a + c2[i][1] * b + c2[i][2] * c; 
  320.         c1[i][1] = c2[i][0] * d + c2[i][1] * e + c2[i][2] * f; 
  321.         c1[i][2] = c2[i][0] * g + c2[i][1] * h + c2[i][2] * j;
  322.       } 
  323. }
  324.  
  325.  
  326.  
  327.  
  328. /*****************************************************************************
  329.   FUNCTION : d3_shiftCube
  330.  
  331.   PURPOSE  : shifts a cube in the upper positive quadrant of the kordsys
  332.   RETURNS  : the new cube
  333.   NOTES    :
  334.  
  335. ******************************************************************************/
  336. void d3_shiftCube (cube c1, cube c2, float x, float y)
  337. {
  338.     int i;
  339.  
  340.     for (i=0; i<ANZ_VECS; i++) {
  341.         c1[i][0] = x + c2[i][0];
  342.         c1[i][1] = y + c2[i][1];
  343.         c1[i][2] = c2[i][2];
  344.         c1[i][3] = c2[i][3];
  345.     }
  346. }
  347.  
  348.  
  349.  
  350.  
  351. /*****************************************************************************
  352.   FUNCTION : d3_shiftVector
  353.  
  354.   PURPOSE  : shifts a vector in the upper positive quadrant of the kordsys
  355.   RETURNS  : the new cube
  356.   NOTES    :
  357.  
  358. ******************************************************************************/
  359. void d3_shiftVector (vector v1, vector v2, float x, float y)
  360. {
  361.     v1[0] = x + v2[0];
  362.     v1[1] = y + v2[1];
  363.     v1[2] = v2[2];
  364.     v1[3] = v2[3];
  365. }
  366.  
  367.  
  368.  
  369.  
  370. /*****************************************************************************
  371.   FUNCTION : d3_projection
  372.  
  373.   PURPOSE  : central projection of a cube
  374.   RETURNS  : the new cube 
  375.   NOTES    :
  376.  
  377. ******************************************************************************/
  378.  
  379. void d3_projection (cube c1, vector observer, cube c)
  380. {
  381.     float zwis;
  382.     int i;
  383.         
  384.     for (i=0; i<ANZ_VECS; i++) {
  385.         zwis = c[i][2] - observer [2];
  386.         c1[i][0] = observer [0] - observer [2] * (c[i][0] - observer [0]) / zwis;
  387.         c1[i][1] = observer [1] - observer [2] * (c[i][1] - observer [1]) / zwis;
  388.     }     
  389. }
  390.  
  391.  
  392. /* end of file */
  393. /* lines: */
  394.  
  395.  
  396.  
  397.  
  398.  
  399.  
  400.  
  401.